package com.his.service.impl;

import com.his.dto.NonDrugForTechnology;
import com.his.entity.LoginUser;
import com.his.entity.NonDrugItemRecord;
import com.his.entity.ResponseResult;
import com.his.mapper.NonDrugItemRecordMapper;
import com.his.mapper.TechnologyMapper;
import com.his.service.ITechnologyService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
public class TechnologyServiceImpl implements ITechnologyService {

    @Resource
    private TechnologyMapper technologyMapper;

    @Resource
    private NonDrugItemRecordMapper nonDrugItemRecordMapper;

    @Override
    public ResponseResult refresh() {
        String message = "";
        Integer code = 0;
        List<NonDrugForTechnology> list = null;

        try{
            list = technologyMapper.getNonDrugItemList();

            message = "拉取列表成功";
            code = 201;
        } catch (Exception err) {
            message = "拉取列表失败";
            code = 400;
        }

        return new ResponseResult(code, message, list);
    }

    @Override
    public ResponseResult register(NonDrugItemRecord nonDrugItemRecord) {
        String message = "";
        Integer code = 0;

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long staffId = loginUser.getUser().getId();

        try{
            nonDrugItemRecord.setLogstaffid(staffId);
            nonDrugItemRecord.setLogstatus(1);
            nonDrugItemRecord.setLogdatetime(LocalDateTime.now());

            if (0 == nonDrugItemRecordMapper.updateById(nonDrugItemRecord)) {
                throw new Exception();
            }

            message = "登记成功";
            code = 201;
        } catch (Exception err) {
            message = "登记失败";
            code = 400;
        }

        return new ResponseResult(code, message);
    }


}
